/*
 * Created by JFormDesigner on Mon Jun 24 15:05:06 CST 2024
 */

package scct.zhanghongbo.ui;
import scct.zhanghongbo.dao.StaffDao;
import scct.zhanghongbo.model.Staff;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 84160
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }
    private static Staff staff = null;
    private void btnLogin(ActionEvent e) {
        String account = txtUser.getText();
        String pass = txtPass.getText();
        System.out.printf("%s,%s\n",account,pass);
        if (!account.matches("^\\w{3,20}$")){
            JOptionPane.showMessageDialog(this,"用户名不合法，请重新输入");
            return;
        }
        if (!pass.matches("^\\w{3,20}$")){
            JOptionPane.showMessageDialog(this,"密码不合法，请重新输入");
            return;
        }
        StaffDao staffDao = new StaffDao();
        staff = staffDao.login(account, pass);
        if (staff == null){
            JOptionPane.showMessageDialog(this,"用户名或密码错误");
            return;
        }
        JOptionPane.showMessageDialog(this,"登录成功");
        FormMain formMain = new FormMain(staff);
        formMain.setSize(800,600);
        formMain.setLocationRelativeTo(null);
        formMain.setVisible(true);
        this.dispose();
    }

    private void txtPassKeyPressed(KeyEvent e) {
        // TODO add your code here
        if(e.getKeyCode()==KeyEvent.VK_ENTER){
            btnLogin.doClick();
        }
    }

    private void btnCancel(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        loginTitle = new JLabel();
        usernameLabel = new JLabel();
        passwordLabel = new JLabel();
        txtUser = new JTextField();
        txtPass = new JPasswordField();
        btnLogin = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setBackground(new Color(0x999999));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setForeground(SystemColor.textInactiveText);
        setIconImage(null);
        setTitle("Login");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- loginTitle ----
        loginTitle.setText("\u767b\u5f55");
        loginTitle.setFont(loginTitle.getFont().deriveFont(loginTitle.getFont().getStyle() | Font.BOLD, loginTitle.getFont().getSize() + 16f));
        contentPane.add(loginTitle);
        loginTitle.setBounds(170, 40, 60, 30);

        //---- usernameLabel ----
        usernameLabel.setText("\u7528\u6237\u540d");
        usernameLabel.setFont(usernameLabel.getFont().deriveFont(usernameLabel.getFont().getStyle() | Font.BOLD, usernameLabel.getFont().getSize() + 3f));
        usernameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        usernameLabel.setLabelFor(txtUser);
        contentPane.add(usernameLabel);
        usernameLabel.setBounds(85, 87, usernameLabel.getPreferredSize().width, 32);

        //---- passwordLabel ----
        passwordLabel.setText("\u5bc6\u7801");
        passwordLabel.setFont(passwordLabel.getFont().deriveFont(passwordLabel.getFont().getStyle() | Font.BOLD, passwordLabel.getFont().getSize() + 3f));
        passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
        passwordLabel.setLabelFor(txtPass);
        contentPane.add(passwordLabel);
        passwordLabel.setBounds(85, 132, 44, 32);
        contentPane.add(txtUser);
        txtUser.setBounds(140, 87, 170, 32);

        //---- txtPass ----
        txtPass.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                txtPassKeyPressed(e);
            }
        });
        contentPane.add(txtPass);
        txtPass.setBounds(140, 132, 170, 32);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.addActionListener(e -> btnLogin(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(100, 200, 80, 35);

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        btnCancel.addActionListener(e -> btnCancel(e));
        contentPane.add(btnCancel);
        btnCancel.setBounds(220, 200, 80, 35);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel loginTitle;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JTextField txtUser;
    private JPasswordField txtPass;
    private JButton btnLogin;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
